import logging
import sys

log = logging.getLogger(__name__)


class NullHandler(logging.Handler):
    """Null logger to avoid spurious messages, add a handler in app code"""

    def emit(self, record):
        pass


h = NullHandler()
log.addHandler(h)


def _init():
    # Add a log filter class
    class StdoutFilter(logging.Filter):
        def filter(self, record):
            # If the record level is 20 (INFO) or lower, let it through
            return record.levelno <= logging.INFO

    # have to create a filter for the stdout stream to filter out WARN+
    myfilt = StdoutFilter()
    # Simple format
    formatter = logging.Formatter("%(message)s")
    stdouthandler = logging.StreamHandler(sys.stdout)
    stdouthandler.addFilter(myfilt)
    stdouthandler.setFormatter(formatter)
    stderrhandler = logging.StreamHandler()
    stderrhandler.setLevel(logging.WARNING)
    stderrhandler.setFormatter(formatter)
    log.addHandler(stdouthandler)
    log.addHandler(stderrhandler)


_init()
